const tsImportPluginFactory = require('ts-import-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const { resolve, isLocal } = require('./utils')
const Theme = require('./theme')

module.exports = [{
  test: /\.(tsx|ts)$/,
  exclude: /node_modules/,
  use: [{
    loader: 'cache-loader',
    options: {
      cacheDirectory: resolve('cache-loader')
    }
  }, {
    loader: 'ts-loader',
    options: {
      transpileOnly: true,
      getCustomTransformers: () => ({
        // @ts-ignore
        before: [tsImportPluginFactory([{
          libraryName: 'antd',
          libraryDirectory: 'lib',
          style: true
        }])]
      })
    }
  }]
}, {
  test: /\.(sc|sa|c)ss$/,
  include: resolve('src'),
  use: [
    {
      loader: isLocal ? 'style-loader' : MiniCssExtractPlugin.loader,
    },
    {
      loader: 'cache-loader',
      options: {
        cacheDirectory: resolve('cache-loader')
      }
    }, {
      loader: 'typings-for-css-modules-loader',
      options: {
        modules: true, // 使用css modules
        namedExport: true, // 类名导出
        camelCase: true, // 支持驼峰
        sass: true, // 是否使用sass
        localIdentName: '[name]_[local]_[hash:base64:5]' // 定义类名
      }
    }, {
      loader: 'postcss-loader',
      options: {
        plugins: [require('autoprefixer')]
      }
    }, {
      loader: 'sass-loader',
      options: {
        sassOptions: {
          includePaths: [resolve('src/styles')]
        }
      }
    }
  ]
}, {
  test: /\.less$/,
  include: [resolve('node_modules/antd'), resolve('node_modules/ad-ui-request')],
  use: [
    isLocal ? 'style-loader' : MiniCssExtractPlugin.loader,
    'css-loader',
    {
      loader: 'less-loader',
      options: {
        modifyVars: Theme,
        javascriptEnabled: true,
      }
    }
  ]
}, {
  test: /\.(png|jpg|gif)$/,
  exclude: /node_modules/,
  use: [{
    loader: 'file-loader',
    options: {
      name: '[name]-[hash:6].[ext]',
      limit: 4028,
      outputPath: 'img/',
      publicPath: `/img`,
      esModule: false
    }
  }]
}, {
  test: /\.(woff|woff2|eot|ttf|otf|svg)$/,
  exclude: /node_modules/,
  use: [{
    loader: 'file-loader',
    options: {
      name: '[name]-[hash:6].[ext]',
      outputPath: 'fonts/',
      publicPath: `/fonts`,
      esModule: false
    }
  }]
}]
